/*
 * UsersList.java
 *
 * Created on 27 sierpień 2007, 22:05
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package pl.krakow.ae.knp.wikiboard.gui.userslist;

import java.awt.Color;
import java.awt.Dimension;
import javax.swing.AbstractListModel;
import javax.swing.BorderFactory;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.ListModel;
import pl.krakow.ae.knp.wikiboard.gui.bundles.Lang;

/**
 *
 * @author slawek
 */
public class UsersList extends JPanel implements UsersListInterface {
    
    private JList usersList;
    private Users users = new Users();
    
    /** Creates a new instance of UsersList */
    public UsersList() {
        addComponents();
        layoutComponents();
    }
    
    private void addComponents() {
        usersList = new JList();
        usersList.setCellRenderer(new UsersListRenderer());
        usersList.setPreferredSize(new Dimension(150, 400));
        usersList.setBorder( BorderFactory.createTitledBorder(
                (BorderFactory.createLineBorder(Color.BLACK)), Lang.get("users_list")));
        setUserlistModel();
    }
    
    private void layoutComponents() {
        add(usersList);
    }
    
    private void setUserlistModel() {
        ListModel listModel = new AbstractListModel() {
            public int getSize() {
                return users.size();
            }
            public Object getElementAt(int i) {
                return users.get(i);
            }
        };
        
        usersList.setModel(listModel);
    }
    
    @SuppressWarnings("unchecked")
    public void addUser(String user) {
        users.add(user);
    }
    
    public void removeUser(String user) {
        users.remove(user);
    }
    
    public void refresh() {
        setUserlistModel();
    }

    public void clearAll() {
        users = new Users();
    }
}
